package demo.spring.restful.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

import demo.spring.restful.domain.Computer;
import demo.spring.restful.domain.Computer;
import demo.spring.restful.service.ComputerService;
import demo.spring.restful.service.ComputerService;

@RestController
public class ComputerController {

    @Autowired
    private ComputerService computerService;  //Service which will do all data retrieval/manipulation work
 
     
    //-------------------Retrieve All Computer--------------------------------------------------------
     
    @RequestMapping(value = "/computer/", method = RequestMethod.GET)
    public ResponseEntity<List<Computer>> listAllComputers() {
        List<Computer> computers = computerService.findAllComputers();
        if(computers.isEmpty()){
            return new ResponseEntity<List<Computer>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
        }
        return new ResponseEntity<List<Computer>>(computers, HttpStatus.OK);
    }
 
 
    //-------------------Retrieve Single Computer--------------------------------------------------------
     
    @RequestMapping(value = "/computer/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Computer> getComputer(@PathVariable("id") long id) {
        System.out.println("Fetching Computer with id " + id);
        Computer computer = computerService.findById(id);
        if (computer == null) {
            System.out.println("Computer with id " + id + " not found");
            return new ResponseEntity<Computer>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<Computer>(computer, HttpStatus.OK);
    }
 
     
     
    //-------------------Create a Computer--------------------------------------------------------
     
    @RequestMapping(value = "/computer/", method = RequestMethod.POST)
    public ResponseEntity<Void> createComputer(@RequestBody Computer computer,    UriComponentsBuilder ucBuilder) {
        System.out.println("Creating Computer " + computer.getName());
 
        if (computerService.isComputerExist(computer)) {
            System.out.println("A Computer with name " + computer.getName() + " already exist");
            return new ResponseEntity<Void>(HttpStatus.CONFLICT);
        }
 
        computerService.saveComputer(computer);
 
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(ucBuilder.path("/computer/{id}").buildAndExpand(computer.getId()).toUri());
        return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
    }
 
     
    //------------------- Update a Computer --------------------------------------------------------
     
    @RequestMapping(value = "/Computer/{id}", method = RequestMethod.PUT)
    public ResponseEntity<Computer> updateComputer(@PathVariable("id") long id, @RequestBody Computer computer) {
        System.out.println("Updating Computer " + id);
         
        Computer currentComputer = computerService.findById(id);
         
        if (currentComputer==null) {
            System.out.println("Computer with id " + id + " not found");
            return new ResponseEntity<Computer>(HttpStatus.NOT_FOUND);
        }
 
        currentComputer.setName(computer.getName());
         
        computerService.updateComputer(currentComputer);
        return new ResponseEntity<Computer>(currentComputer, HttpStatus.OK);
    }
 
    //------------------- Delete a Computer --------------------------------------------------------
     
    @RequestMapping(value = "/computer/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<Computer> deleteComputer(@PathVariable("id") long id) {
        System.out.println("Fetching & Deleting Computer with id " + id);
 
        Computer computer = computerService.findById(id);
        if (computer == null) {
            System.out.println("Unable to delete.Computer with id " + id + " not found");
            return new ResponseEntity<Computer>(HttpStatus.NOT_FOUND);
        }
 
        computerService.deleteComputerById(id);
        return new ResponseEntity<Computer>(HttpStatus.NO_CONTENT);
    }
 
     
    //------------------- Delete All Computers --------------------------------------------------------
     
    @RequestMapping(value = "/computer/", method = RequestMethod.DELETE)
    public ResponseEntity<Computer> deleteAllComputers() {
        System.out.println("Deleting All Computers");
 
        computerService.deleteAllComputers();
        return new ResponseEntity<Computer>(HttpStatus.NO_CONTENT);
    }
}
